var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");

var kelingqu = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("kelingqu go_page_from_home: ");
    logger("SUCCESS");
    // if (this.is_over_today) {
    //   logger("今天的广告任务已全部执行完毕");
    //   return;
    // }
    // utils.click_point.apply(
    //   utils,
    //   utils.get_size_point(point_map["首页赚钱"], 1080, 2340)
    // );
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    this.init();
  },
  init() {
    var logger = utils.create_logger("kelingqu init: ");
    logger();
    var ocr_text = ocr_utils.find_text(
      "可领取",
      false,
      null,
      1,
      (device.height / 10) * 3
    );
    if (ocr_text) {
      logger("找到--可领取--");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      var ocr_text = ocr_utils.capture_exists_text("看广告视频");
      if (ocr_text) {
        logger("找到-" + utils.str_format("看广告视频") + "-");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(5, 0, 2, 1000));
        if (utils.exists_text_starts_with("反馈")) {
          logger("找到--反馈--");
          this.run();
        } else {
          this.is_over_today = true;
          logger("没找到--反馈--");
          this.after();
        }
      } else {
        logger("没找到-" + utils.str_format("看广告视频") + "-");
        this.after();
      }
    } else {
      logger("没找到--可领取--");
      this.after();
    }
  },
  run() {
    ad.has_watch_ad_times = 0;
    ad.init();
    this.after();
  },
  after() {
    console.log("kelingqu after");
    if (utils.click_text_starts_with("开心收下")) {
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    console.log("kelingqu next");
  },
};
module.exports = kelingqu;
